package android_testsuite.mytest.rsa;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import android_testsuite.R;

/**
 * @author Ren Hui
 * @since 1.0.1.058
 */
public class RsaActivity extends Activity {
    //加密按钮
    private Button mEnButton;
    //解密按钮
    private Button mDeButton;
    //结果显示
    private TextView mResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rsa);
        this.mEnButton = (Button) findViewById(R.id.encrypt);
        this.mDeButton = (Button) findViewById(R.id.decrypt);
        this.mResult = (TextView) findViewById(R.id.result);

        mEnButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HandleDateTask task = new HandleDateTask();
                task.execute(true);
            }
        });

        mDeButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                HandleDateTask task = new HandleDateTask();
                task.execute(false);
            }
        });
    }

    private class HandleDateTask extends AsyncTask<Boolean, Void, String> {
        //后台运行
        @Override
        protected String doInBackground(Boolean... booleans) {
            String result;
            if (booleans[0] == true) {
                //加密
                RsaEncrypt r = new RsaEncrypt(RsaActivity.this);
                result = r.encodeToBase64();
            } else {
                //解密
                RsaDecoder d = new RsaDecoder(RsaActivity.this);
                result = d.decodeBase64();
            }
            return result;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            mResult.setText(s);
        }
    }

}
